import { HOME_GET_CONFIG, HOME_GET_COURSE } from '../mutation-types';
import { getHomeConfigApi, getHomeCourseApi } from '../../api';

const state = {
  broadcast: [],
  recommend: [],
  course: []
};

const getters = {
  broadcast: (state) => state.broadcast,
  recommend: (state) => state.recommend,
  course: (state) => state.course
};

const mutations = {
  [HOME_GET_CONFIG](state, payload) {
    state.broadcast = payload.broadcast;
    state.recommend = payload.recommend;
  },
  [HOME_GET_COURSE](state, payload) {
    state.course = payload.course;
  }
};

const actions = {
  [HOME_GET_CONFIG]({ commit }) {
    getHomeConfigApi()
      .then(res => {
        commit({
          type: HOME_GET_CONFIG,
          broadcast: res.data.broadcast,
          recommend: res.data.recommend
        });
      });
  },
  [HOME_GET_COURSE]({ commit }, data) {
    getHomeCourseApi(data.code)
      .then(res => {
        console.log('首页数据刷新----> : ', res);
        const data = res.data;
        const course = [];
        // 根据自己设计的模型 加工数据
        data.map((cur) => {
          cur['banner'] = cur.recommends[0];
          cur['data'] = cur.recommends.slice(1);
          course.push(cur);
        });
        console.log(data);
        console.log(course);
        commit({
          type: HOME_GET_COURSE,
          course: course
        });
      });
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
